package com.leetcode.Offer;

public class JZ47 {
    public int maxValue(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }

        //动态规划：每一步都选择最优
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (i > 0 && j > 0) {//上边和左边都有数值
                    grid[i][j] = grid[i][j] + Math.max(grid[i - 1][j], grid[i][j - 1]);
                } else if (i > 0) {//上边有数值
                    grid[i][j] = grid[i][j] + grid[i - 1][j];
                } else if (j > 0) {//左边有数值
                    grid[i][j] = grid[i][j] + grid[i][j - 1];
                }
            }
        }
        return grid[grid.length - 1][grid[0].length - 1];
    }
}
